home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / net / amitcp2_x_gcc.lha / RCS.RCSfiles / getopt.c,v < prev    next >
Text File  |  1994-01-11  |  3KB  |  131 lines

  1. head    1.2;
  2. access;
  3. symbols;
  4. locks
  5.     jasegler:1.2; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    94.01.11.19.51.38;    author jasegler;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    94.01.11.19.04.47;    author jasegler;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @*** empty log message ***
  27. @
  28. text
  29. @/*
  30.    **   @@(#)getopt.c    2.5 (smail) 9/15/87
  31.  */
  32.  
  33. /*
  34.  * Here's something you've all been waiting for:  the AT&T public domain
  35.  * source for getopt(3).  It is the code which was given out at the 1985
  36.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  37.  * directly from AT&T.  The people there assure me that it is indeed
  38.  * in the public domain.
  39.  *
  40.  * There is no manual page.  That is because the one they gave out at
  41.  * UNIFORUM was slightly different from the current System V Release 2
  42.  * manual page.  The difference apparently involved a note about the
  43.  * famous rules 5 and 6, recommending using white space between an option
  44.  * and its first argument, and not grouping options that have arguments.
  45.  * Getopt itself is currently lenient about both of these things White
  46.  * space is allowed, but not mandatory, and the last option in a group can
  47.  * have an argument.  That particular version of the man page evidently
  48.  * has no official existence, and my source at AT&T did not send a copy.
  49.  * The current SVR2 man page reflects the actual behavor of this getopt.
  50.  * However, I am not about to post a copy of anything licensed by AT&T.
  51.  */
  52.  
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <stdio.h>
  56.  
  57. /*LINTLIBRARY */
  58. #define EOF    (-1)
  59. #define ERR(s, c)\
  60.   if(opterr) { fprintf(stderr, "%s%s%lc\n", argv[0], s, c); }
  61.  
  62. int opterr = 1;
  63. int optind = 1;
  64. int optopt;
  65. char *optarg;
  66.  
  67. int
  68. getopt (int argc, char *const *argv, const char *opts)
  69. {
  70.   static int sp = 1;
  71.   register long int c;
  72.   register char *cp;
  73.  
  74.   if (sp == 1)
  75.     if (optind >= argc ||
  76.     argv[optind][0] != '-' || argv[optind][1] == '\0')
  77.       return (EOF);
  78.     else if (strcmp (argv[optind], "--") == NULL)
  79.       {
  80.     optind++;
  81.     return (EOF);
  82.       }
  83.   optopt = c = argv[optind][sp];
  84.   if (c == ':' || (cp = index (opts, c)) == NULL)
  85.     {
  86.       ERR (": illegal option -- ", c);
  87.       if (argv[optind][++sp] == '\0')
  88.     {
  89.       optind++;
  90.       sp = 1;
  91.     }
  92.       return ('?');
  93.     }
  94.   if (*++cp == ':')
  95.     {
  96.       if (argv[optind][sp + 1] != '\0')
  97.     optarg = &argv[optind++][sp + 1];
  98.       else if (++optind >= argc)
  99.     {
  100.       ERR (": option requires an argument -- ", c);
  101.       sp = 1;
  102.       return ('?');
  103.     }
  104.       else
  105.     optarg = argv[optind++];
  106.       sp = 1;
  107.     }
  108.   else
  109.     {
  110.       if (argv[optind][++sp] == '\0')
  111.     {
  112.       sp = 1;
  113.       optind++;
  114.     }
  115.       optarg = NULL;
  116.     }
  117.   return (c);
  118. }
  119. @
  120.  
  121.  
  122. 1.1
  123. log
  124. @Initial revision
  125. @
  126. text
  127. @d43 1
  128. a43 1
  129.   register int c;
  130. @
  131.